//
//  VideoList.swift
//  Bilibili
//
//  Created by admin on 2020/9/28.
//  Copyright © 2020 MBA. All rights reserved.
//

import SwiftUI
import Alamofire
import SDWebImageSwiftUI

struct VideoList: View {
    
    @State var videoList = [VideoListModel]()
    @State var selsctitem: VideoListModel?
    @State var loading = false
    
    
    init(){
        UITableView.appearance().backgroundColor = .clear
        UITableViewCell.appearance().backgroundColor = .clear
        //若不要row分隔线的话：
        UITableView.appearance().separatorStyle = .none
    }
    
    func getvideos()  {
        loading = true
        AF.api_videoList(finesh:{ (error, results) in
            if((error) != nil){
                print(error?.description as Any)
            }else{
                
                self.videoList = results ?? [VideoListModel]()
            }
            loading = false
        })
    }
    var body: some View {
        
        NavigationView{
            ZStack {
                
                let columns: [GridItem] =
                    Array(repeating: .init(.flexible()), count: 1)
                
                ScrollView {
                    
                    LazyVGrid(columns: columns, spacing:10) {
                        ForEach(videoList.indices, id: \.self) { index in
                            
                            VideoCell(video: videoList[index])
                                .padding(.horizontal)
                        }
                    }
                }
                if loading == true{
                    LoadingView()
                        .background(Color.black.opacity(0.3))
                        .edgesIgnoringSafeArea(.all)
                }
            }
            .onAppear(perform: {
                self.getvideos()
            })
            .navigationBarTitle(Text("首页"))
        }
        
    }
}

struct VideoList_Previews: PreviewProvider {
    static var previews: some View {
        VideoList()
    }
}




